package us.language.translate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;

public class MicrosoftTranslate implements Translate {

	@Override
	public ArrayList<String> getAvailableLanguages() throws IOException {
		String s = "http://api.microsofttranslator.com/V1/Http.svc/GetLanguageNames?appId=09D2092E8E98F63E87509A9EAA1A9DA3FC374BC4&locale=en";
		String returnXML = httpConnectionResult(s);
		ArrayList<String> supportedLanguages=(ArrayList<String>) Arrays.asList(returnXML.split("\n"));
		return supportedLanguages;
	}

	@Override
	public String translate(String text, String translateFrom,
			String translateTo) {
		// TODO Auto-generated method stub
		return null;
	}

	private String httpConnectionResult(String url) throws IOException {
		URL u = new URL(url);
		HttpURLConnection h;
		h = (HttpURLConnection) u.openConnection();
		h.setDoInput(true);
		InputStream o = h.getInputStream();
		BufferedReader in = new BufferedReader(new InputStreamReader(o));
		StringBuilder sb = new StringBuilder();
		String input = in.readLine();
		while (input != null) {
			sb.append(input);
			input = in.readLine();
		}
		return sb.toString();
	}
/*
	private AllLanguages changeToFullName(String shorthand) {
		String[] languages={"ar","bg","ca","zh-CHS","zh-CHT","cs","da","nl","en","et","fi","fr","de","el","ht","he","hu","id","it","ja","ko","lv","lt","no","pl","pt","ro","ru","sk","sl","es","sv","th","tr","uk","vi"};
		int match=-1;
		for(int i=0; i<languages.length; i++)
		{
			if(languages[i].equals(shorthand))
			{
				match=i;
			}
		}
		switch(match)
		{
			case -1: return null;
			case 0: return AllLanguages.ARABIC;
			case 1: return AllLanguages.BULGARIAN;
			case 2: return AllLanguages.CATALAN;
			case 3: return AllLanguages.CHINESE_SIMPLIFIED;
			case 4: return AllLanguages.CHINESE_TRADITIONAL;
			case 5: return AllLanguages.CZECH;
			case 6: return AllLanguages.DANISH;
			case 7: return AllLanguages.DUTCH;
			case 8: return AllLanguages.ENGLISH;
			case 9: return AllLanguages.ESTONIAN;
			case 10: return AllLanguages.FINNISH;
			case 11: return AllLanguages.FRENCH;
			case 12: return AllLanguages.GERMAN;
			case 13: return AllLanguages.GREEK;
			case 14: return AllLanguages.HAITIAN_CREOLE;
			case 15: return AllLanguages.HEBREW;
			case 16: return AllLanguages.HUNGARIAN;
			case 17: return AllLanguages.INDONESIAN;
			case 18: return AllLanguages.ITALIAN;
			case 19: return AllLanguages.JAPANESE;
			case 20: return AllLanguages.KOREAN;
			case 21: return AllLanguages.LATVIAN;
			case 22: return AllLanguages.LITHUANIAN;
			case 23: return AllLanguages.NORWEGIAN;
			case 24: return AllLanguages.POLISH;
			case 25: return AllLanguages.PORTUGUESE;
			case 26: return AllLanguages.ROMANIAN;
			case 27: return AllLanguages.RUSSIAN;
			case 28: return AllLanguages.SLOVAK;
			case 29: return AllLanguages.SLOVENIAN;
			case 30: return AllLanguages.SPANISH;
			case 31: return AllLanguages.SWEDISH;
			case 32: return AllLanguages.THAI;
			case 33: return AllLanguages.TURKISH;
			case 34: return AllLanguages.UKRANINIAN;
			case 35: return AllLanguages.VIETNAMESE;
		}
		return null;
	}
*/
}
